package com.wang.mall.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wang.mall.vo.PayVo;
import org.springframework.stereotype.Component;

//@ConfigurationProperties(prefix = "alipay")
@Component
//@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2021000117642327";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtjCwZiUxBxReY0i3CJpeOXOfeA6E/H7vMxdJSpJuQk/NF620HyhaHDwsY1GDuLAE5q6ZAG13+tjLLOxWZWvXhNUYoNWnJC4r34xuVfRwM82LzcPvE1s0sd3P5mDzQdDCrCSzSWpfGWNfXhqfvX6DeT7YSlWujByusvQX4VqBvvvQ9QbBbeD6DHnW3Lr7YOz3Dq81TNUjvew3SRge76ze7MmoctnmKB/fj2c7kuheRnGyqdRbLYK1kk0bJ+3MX2xVaNZiwZ52p+3ijf1o1SvYC4Y0QpqQFiEU5NvdkziuAoltXcoEQzZn49bWw+poIji0nBfF+jqcbbATWcfGmnichAgMBAAECggEAcddXPlEQEryk4/xLQ+Zo2FwdRUeASJYabRL/CnMsEMTceesbFFb+PA4kK191TgbTru7oGXlaSTFfVG0nl/dQ/zAhG1MeV2EZAc6aU49EDULCkAp3PQHiiN661umESwFMw4U7P6lpkhTJUXnlQ2Kpdes39izJ9KFJhwBfoC3DBElXjdPLO/XheSM9beczZM6HEuDLy4Qe1oazTwkiNDenX8pi3aKdSiVYDw/7XGHNxB6rJRVwqCNzXgEGCpNBzSY67+4G0CIJ6hnou3ChUHKRv36WMcYexAFgL9nPpm3AlYxbd9VBkS9pIq/2fGWF6G4Pw8AYTfZ/GqJRA2E0ah5IoQKBgQD8TOD5bAz2VDFZ7cQ3CnfoEggLrCPnauTMrULw+P+tm3o76VpLz7eyZ7vlhQAtQGqEk+75zwYsBul5PpPdeSOftIDvy0fuDxZkAHmThHS7P42iuX6vzo5GlEODsIsW5ExM/AzDD36DXnuJNScA5cTDUtAVFkuEXzQLQdxE2jp+tQKBgQCwF6j29qdPelisiv5PkN50NdCXt26xEjhrJt4KztGaQMU6ygJx8TnCwtMjw+dyBGwQ9rWfbH6827nC/rH1ZU+8sB75P8aTsx7NXGCz2uQOO2lqEgGW5rEtaBeZ3I5nU/qVSxazZx6aEAgkks/ztbShNdv3Q267HRuesu3tbLnePQKBgQDo49I7iljAvOC9QTQBFqGuT46rr5+Iy2wd3sVPCJmCWGVRjkqydi/797NC3y1VmAN7E87hOw9YbpzvNStqoZqXnhfywXNJNRYXAzU2TWsvOS/1iEJ/6Cv+zHcxxTHu1Jf17CefmtrOStW/xpeVRYMGRZgcDvNwYiz5QzcfO04JjQKBgGwmq+y6QroKn4mheF3v0OC46Js12vNACIyu+3LR4WQG4duglG3cxSf8Nmdh+7VISAMHMzduEEz3+phri2r3RUjTNsLIy7KNMZ7ZXL858hVXof+2fWlr2GfV5aDnGEMYDfBBhPi8ncvrisTDspqw//Z/ONEdhlW/o+YNaRoITAPVAoGAOWso7takLpoUVX8aq9RK1kPYZi+8prxyhNQTxdDYj1RNv98CnvWdeihQrnMKFwfg5nnehNyhLQmnTHoGdTGQvL7/XdckF6AnzFMJFNbsWwEd9igiscJCeKmyUxzDF/4VA6k31VedMb6NYbgdcCEj3xwHi1L5Tx1PJNHZeEXXJhY=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoKI7NyQIhcu12uFsIo/au4w9+u+8+l2AGmDhRkIktR8zkyk/gFGbHGa2YLQ1PSY1nDAvmt37Hm9+d4IsUTo4A9fh47jzUiAQPJ0GOCEvWQJMI8cU4TcmfYUbGk+RQHJ8TngROCk8bsDuyIOdhb5RcFyIhZkXyZPqdhu0/4nWVxEFLomfX/LYhCHyDjDmSd+XZAzkROZHWZVlFcdHJQ0au+HBL+0sfsgtLoTzRWyA+QgP6Gh3neFAE1jjazmSSLWStYJm02E4D71xEL0RchdTQrdvsmVNrfjSUFPglOOLOGVyRLQAiGKi1Y0rW65IjJlOeT+Hfm9DEwpHvv6C5zZlRwIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://3496h263w4.zicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://127.0.0.1:9999/paysuccess.html";

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public static String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String timeout = "1m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
       // System.out.println("支付宝的响应：" + result);

        return result;

    }
}
